package com.cafuc.io;

import org.apache.commons.io.FileUtils;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.*;
import java.util.List;
import java.util.Properties;

public class FileOperation {
    public void fileTest(){
        File file = new File("E:\\temp\\temp.txt");
        System.out.println(file.exists());
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getName());
        File temp = new File("E:\\temp\\temp2");
        if(!temp.exists()) {
            temp.mkdirs();
        }
    }

    public void filePathTest(){
        System.out.println(FileOperation.class.getResource("/test.txt").getPath());
        System.out.println(FileOperation.class.getClassLoader().getResource("test.txt").getPath());
    }

    public void readFile(String filePath){
        InputStream is = null;
        int buffer = 0;
        StringBuffer sb;
        String line = "";
        BufferedReader br = null;
        try {
            //使用 inputStream 读取文件
            is = new FileInputStream(filePath);
            buffer = 0;
            sb = new StringBuffer();
            while((buffer = is.read()) != -1){
                sb.append((char)buffer);
            }
            System.out.println(sb.toString());

            sb = new StringBuffer();
            is = new FileInputStream(filePath);
            br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
            while((line = br.readLine()) != null){
                sb.append(line).append("\r\n");
            }
            System.out.println(sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(is != null) {
                    is.close();
                }
                if(br!= null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void writeFile(String filePath, String content){
        OutputStream os = null;
        OutputStreamWriter osw = null;

       // File file;
        try {
            //使用字节流写文件
            os = new FileOutputStream(filePath);
            os.write(content.getBytes("UTF-8"));
            os.flush();

            //使用字符流写文件
            osw = new OutputStreamWriter(os, "UTF-8");
            osw.write(content, 0, content.length());
            osw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(os != null){
                    os.close();
                }
                if(osw != null){
                    osw.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void commonsIOTest(String filePath, String content){
        try {
            List<String> lines = FileUtils.readLines(
                    new File(filePath),"UTF-8");
            System.out.println(lines);
            FileUtils.writeStringToFile(new File(filePath), content, "UTF-8");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void propertiesTest(String filePath){
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
            System.out.println(properties.get("jdbc.driverClassName"));
            System.out.println(properties.get("jdbc.url"));
            System.out.println(properties.get("jdbc.username"));
            System.out.println(properties.get("jdbc.password"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        FileOperation fo = new FileOperation();
        //fo.fileTest();
        fo.filePathTest();
        String filePath = "E:\\temp\\temp.txt";
        String content = "纵容着任性的随意的放肆的轻易的\n\r 将所有欢脱倾翻 hgfhsghs";
        fo.readFile(filePath);
        fo.writeFile(filePath, content);
        fo.commonsIOTest(filePath, content);
        fo.propertiesTest(FileOperation.class.getResource("/jdbc.properties").getPath());
    }
}
